/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import java.sql.*;
import java.util.logging.*;

/**
 *
 * @author Boriz
 */
public class Pemesan {

    private String nama;
    private String alamat;
    private String notlp;
    private String email;
    private String tanggal;
    private String gender;
    private String paket;
    private String id;
    private String status ; 
    private String koneksi = "rplopesanan";
    private String username = "MHS105314093";
    private String password = "MHS105314093";
    private Connection con = null;
    private Statement stmt = null;
    private ResultSet rs = null;
    private DatabaseMetaData dmd;
    private ResultSetMetaData rsmd;

    public String aksesDatabase() {
        String dbHTML = "";

        if (getKoneksi() == null) {
            // clear password
            setPassword("");
            return dbHTML;
        }
        if (getKoneksi().equals("")) {
            // clear password
            setPassword("");
            dbHTML = "Error koneksi. Nama tabel kosong.";
            return dbHTML;
        }
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
//            setCon(DriverManager.getConnection("jdbc:Oracle:thin:@localhost:1521:XE ", getUsername(), getPassword()));
            setCon(DriverManager.getConnection("jdbc:Oracle:thin:@172.23.9.185:1521:orcl ", getUsername(), getPassword()));

            setStmt(getCon().createStatement());

            setRs(getStmt().executeQuery("SELECT * FROM " + getKoneksi()));
            setDmd(getCon().getMetaData());
            dbHTML += "<p><i>Database: </i>" + getDmd().getDatabaseProductName()
                    + " v." + getDmd().getDatabaseProductVersion();

            dbHTML += "<br/><i>JDBC Driver-name: </i>" + getDmd().getDriverName();
            dbHTML += "<br/><i>Version: </i>" + getDmd().getDriverVersion() + "</p>";

            setRsmd(getRs().getMetaData());
            int nColumn = getRsmd().getColumnCount();

            dbHTML += "<table border=\"1\" cellpadding=\"4\"><tr>";

            for (int i = 1; i <= nColumn; i++) {
                dbHTML += "<th>" + getRsmd().getColumnName(i) + "</th>";
            }
            dbHTML += "</tr>";
            while (getRs().next()) {
                dbHTML += "<tr>";
                for (int i = 1; i <= nColumn; i++) {
                    dbHTML += "<td>" + getRs().getString(i) + "</td>";
                }
                dbHTML += "</tr>";
            }

            dbHTML += "</table>";
        } catch (ClassNotFoundException e) {
            dbHTML += "<p>Error loading driver: " + e.getMessage() + "</p>";
        } catch (SQLException e) {
            dbHTML += "<p>Error SQL: " + e.getMessage() + "</p>";
        } finally {
            try {
                if (getRs() != null) {
                    getRs().close();
                }
            } catch (SQLException sqle) {
            }
            try {
                if (getStmt() != null) {
                    getStmt().close();
                }
            } catch (SQLException sqle) {
            }
            try {
                if (getCon() != null) {
                    getCon().close();
                }
            } catch (SQLException sqle) {
            }
        }
        setPassword("");
        return dbHTML;
    }

     public String aksesDatabasekonfirmasi() {
        String dbHTML = "";

        if (getKoneksi() == null) {
            // clear password
            setPassword("");
            return dbHTML;
        }
        if (getKoneksi().equals("")) {
            // clear password
            setPassword("");
            dbHTML = "Error koneksi. Nama tabel kosong.";
            return dbHTML;
        }
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            setCon(DriverManager.getConnection("jdbc:Oracle:thin:@localhost:1521:XE ", getUsername(), getPassword()));
           // setCon(DriverManager.getConnection("jdbc:Oracle:thin:@172.23.9.185:1521:orcl ", getUsername(), getPassword()));

            setStmt(getCon().createStatement());

            setRs(getStmt().executeQuery("select nama, paket,alamat, tanggal from rplopesanan where tanggal = '';" + getKoneksi()));
            setDmd(getCon().getMetaData());
            dbHTML += "<p><i>Database: </i>" + getDmd().getDatabaseProductName()
                    + " v." + getDmd().getDatabaseProductVersion();

            dbHTML += "<br/><i>JDBC Driver-name: </i>" + getDmd().getDriverName();
            dbHTML += "<br/><i>Version: </i>" + getDmd().getDriverVersion() + "</p>";

            setRsmd(getRs().getMetaData());
            int nColumn = getRsmd().getColumnCount();

            dbHTML += "<table border=\"1\" cellpadding=\"4\"><tr>";

            for (int i = 1; i <= nColumn; i++) {
                dbHTML += "<th>" + getRsmd().getColumnName(i) + "</th>";
            }
            dbHTML += "</tr>";
            while (getRs().next()) {
                dbHTML += "<tr>";
                for (int i = 1; i <= nColumn; i++) {
                    dbHTML += "<td>" + getRs().getString(i) + "</td>";
                }
                dbHTML += "</tr>";
            }

            dbHTML += "</table>";
        } catch (ClassNotFoundException e) {
            dbHTML += "<p>Error loading driver: " + e.getMessage() + "</p>";
        } catch (SQLException e) {
            dbHTML += "<p>Error SQL: " + e.getMessage() + "</p>";
        } finally {
            try {
                if (getRs() != null) {
                    getRs().close();
                }
            } catch (SQLException sqle) {
            }
            try {
                if (getStmt() != null) {
                    getStmt().close();
                }
            } catch (SQLException sqle) {
            }
            try {
                if (getCon() != null) {
                    getCon().close();
                }
            } catch (SQLException sqle) {
            }
        }
        setPassword("");
        return dbHTML;
    }

    /**
     * @return the nama
     */
    public String getNama() {
        return nama;
    }

    /**
     * @param nama the nama to set
     */
    public void setNama(String nama) {
        this.nama = nama;
    }

    /**
     * @return the alamat
     */
    public String getAlamat() {
        return alamat;
    }

    /**
     * @param alamat the alamat to set
     */
    public void setAlamat(String alamat) {
        this.alamat = alamat;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return the notlp
     */
    public String getNotlp() {
        return notlp;
    }

    /**
     * @param notlp the notlp to set
     */
    public void setNotlp(String notlp) {
        this.notlp = notlp;
    }

    /**
     * @return the koneksi
     */
    public String getKoneksi() {
        return koneksi;
    }

    /**
     * @param koneksi the koneksi to set
     */
    public void setKoneksi(String koneksi) {
        this.koneksi = koneksi;
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the con
     */
    public Connection getCon() {
        return con;
    }

    /**
     * @param con the con to set
     */
    public void setCon(Connection con) {
        this.con = con;
    }

    /**
     * @return the stmt
     */
    public Statement getStmt() {
        return stmt;
    }

    /**
     * @param stmt the stmt to set
     */
    public void setStmt(Statement stmt) {
        this.stmt = stmt;
    }

    /**
     * @return the rs
     */
    public ResultSet getRs() {
        return rs;
    }

    /**
     * @param rs the rs to set
     */
    public void setRs(ResultSet rs) {
        this.rs = rs;
    }

    /**
     * @return the dmd
     */
    public DatabaseMetaData getDmd() {
        return dmd;
    }

    /**
     * @param dmd the dmd to set
     */
    public void setDmd(DatabaseMetaData dmd) {
        this.dmd = dmd;
    }

    /**
     * @return the rsmd
     */
    public ResultSetMetaData getRsmd() {
        return rsmd;
    }

    /**
     * @param rsmd the rsmd to set
     */
    public void setRsmd(ResultSetMetaData rsmd) {
        this.rsmd = rsmd;
    }

    /**
     * @return the gender
     */
    public String getGender() {
        return gender;
    }

    /**
     * @param gender the gender to set
     */
    public void setGender(String gender) {
        this.gender = gender;
    }

    /**
     * @return the tanggal
     */
    public String getTanggal() {
        return tanggal;
    }

    /**
     * @param tanggal the tanggal to set
     */
    public void setTanggal(String tanggal) {
        this.tanggal = tanggal;
    }

    /**
     * @return the paket
     */
    public String getPaket() {
        return paket;
    }

    /**
     * @param paket the paket to set
     */
    public void setPaket(String paket) {
        this.paket = paket;
    }

    /**
     * @return the bayar
     */
    public String getId() {
        return id;
    }

    /**
     * @param bayar the bayar to set
     */
    public void setId(String id) {
        this.id = id;
    }

    public final String ID_Pemesan() {
        try {
            String sql = "select max(IDPESANAN) from RPLOPESANAN";
            Connection conn = DataBaseConnection.KoneksiDataBase();
            java.sql.Statement sta = conn.createStatement();
            ResultSet res = sta.executeQuery(sql);
            res.next();
            String result = res.getString(1);
            if (result != null) {
                int no = Integer.parseInt(result);
                no++;
                sql = "" + no;
                while (sql.length() <= 3) {
                    sql = "0" + sql;
                }
            } else {
                sql = "0100";
            }
            return sql;
        } catch (SQLException ex) {
            Logger.getLogger(Pemesan.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    /**
     * @return the status
     */
    public String getStatus() {
        return status;
    }

    /**
     * @param status the status to set
     */
    public void setStatus(String status) {
        this.status = status;
    }
    
    public static void main(String[] args) {
        new Pemesan().ID_Pemesan();
    }
}
